import axios from 'axios'
import format from './format'
import config from '../configs'
import { guid } from './utils'


const initData = (data) => {
    var tree = [];
    var treeFolder = {};
    var treeItem = {};
    data.forEach(ele => {
        var folder = {};
        folder.id = guid();
        folder.title = ele.info.title;
        // console.log(folder.title);
        folder.children = [];
        folder.models = {};
        folder.Schemas = {};
        var _models = ele.components.schemas;
        for (const key in _models) {
            const element = _models[key];
            folder.Schemas[key] = element;
            folder.models['#/components/schemas/' + key] = format.Models(element, key);
        };
        for (const url in ele.paths) {
            const method = ele.paths[url];
            for (const type in method) {
                const _file = method[type];
                var file = {};
                file.title = _file.summary;
                file.url = url;
                file.type = type;
                file.id = file.type + '-' + file.url;
                file.key = _file.tags[0];
                file.query = _file.parameters;
                _file.requestBody && (() => {
                    file.body = {};
                    for (const key in _file.requestBody.content) {
                        const element = _file.requestBody.content[key];
                        if (key != 'application/*+json' && key != 'text/json' && key != 'application/json-patch+json') {
                            file.body[key] = format.Models(element.schema, key);
                        }
                    }
                })();
                file.parent = folder.id;
                _file.responses && (function () {
                    if (!_file.responses) {
                        return;
                    }
                    if (!_file.responses['200']) {
                        return;
                    }
                    if (!_file.responses['200'].content) {
                        return;
                    }
                    if (!_file.responses['200'].content['application/json']) {
                        return;
                    }
                    if (!_file.responses['200'].content['application/json'].schema) {
                        return;
                    }
                    file.responses = format.Models(_file.responses['200'].content['application/json'].schema, '200');
                })();
                folder.children.push(file);
                if (treeItem[file.id]) {
                    console.log(file.id);
                };
                treeItem[file.id] = file;
            };
        };
        tree.push(folder);
        treeFolder[folder.id] = folder;
    });
    return { tree, treeFolder, treeItem }
}

const getDoc = (url) => {
    let reg = /configObject\s+=\s+JSON\.parse\((.+?)\)/;
    let IP = url.split('/')[2];
    let _IP = 'http://' + IP;
    return axios.get(url).then((res) => {
        let configObject;
        res.data.replace(reg, function ($i) {
            eval($i);
        });
        let requireArray = [];
        for (let i = 0; i < configObject.urls.length; i++) {
            const element = configObject.urls[i];
            requireArray.push(axios.get(_IP + element.url).then(res => res.data));
        }
        return axios.all(requireArray);
    }).then(res => initData(res)).then((res) => {
        return {
            ...res,
            _IP,
        }
    })
};

const getDevDoc = (url) => {
    let IP = url.split('/')[2];
    let _IP = 'http://' + IP;

    return new Promise((resolve, reject) => {
        // 一段耗时的异步操作
        // console.log(config);


        var _data = initData(config);
        resolve({ ..._data, _IP }) // 数据处理完成
        // reject('失败') // 数据处理出错
    })
};
export {
    getDoc,
    getDevDoc,
}